﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataLayer.Entities;
using DataLayer.Implementations;
using DonutMgmtSystemFoundation.Helpers;
using DonutMgmtSystemFoundation.Models;

namespace DonutMgmtSystemFoundation.Controllers
{
    public class LoginController : BaseController
    {
        [HttpGet]
        [ActionName("Login")]
        public ActionResult LoginGet(User user)
        {
            return View("Index", new GeneralJsonResultModel(true,""));
        }

        [HttpPost]
        public ActionResult Login(User user)
        {
            LoginHelper helper = new LoginHelper();
            GeneralJsonResultModel result = helper.LoginUser(user);
            if (result.Success)
            {
                return RedirectToAction("Index","Home");
            }
            return View("Index", result);
        }

        public ActionResult ForgetPassword()
        {
            return View();
        }

        [HttpPost]
        public JsonResult ForgetPasswordPost(string userOrEmail)
        {
            LoginHelper helper = new LoginHelper();
            return Json(helper.GenerateForgetPasswordRequest(userOrEmail));
        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public JsonResult RegisterPost(User user)
        {
            // Validar usuario para registro
            LoginHelper helper = new LoginHelper();
            return Json(helper.RegisterUser(user));
        }

        public ActionResult Activate(ActivateUserModel model)
        {
            LoginHelper helper = new LoginHelper();
            return View(helper.ActivateUser(model));
        }

        public ActionResult RecoverPassword(ActivateUserModel model)
        {
            LoginHelper helper = new LoginHelper();
            ViewBag.userName = model.Username;
            return View(helper.RecoverPassword(model));
        }

        [HttpPost]
        public JsonResult RecoverPasswordPost(User user)
        {
            LoginHelper helper = new LoginHelper();
            return Json(helper.ChangePassword(user.Username, user.Password));
        }

        public ActionResult Logout()
        {
            SessionContext.Logout();
            return View("Index", new GeneralJsonResultModel(true, ""));
        }
    }
}
